/**
 * Copyright (C), 2021, 因特驰
 * FileName: SellerController
 * Author:   李晋武
 * Date:     2021/7/13 17:07
 * Description:
 * History:
 * 李晋武          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.esc.controller;

import com.esc.model.Seller;
import com.esc.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 〈卖家〉<br>
 * 〈〉
 *
 * @author Yun
 * @create 2021/7/13
 * @since 1.0.0
 */
@Controller
@RequestMapping("seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @GetMapping("index")
    public String index(){
        return "seller/index";
    }

    //登录
    @RequestMapping("login")
    @ResponseBody
    public Map<String,String> login(String checkCode, String userPhone, String userPwd, HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        //校验验证码是否正确
        if(!request.getSession().getAttribute("checkcode").equals(checkCode)){
            map.put("code","1");
            map.put("msg","验证码不正确，请重新输入");
            return map;
        }
        Seller seller = sellerService.querySellerByPhone(userPhone);
        if(seller==null){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        if(!seller.getSellerpwd().equals(userPwd)){
            map.put("code","2");
            map.put("msg","用户名或密码不正确，请重新输入");
            return map;
        }
        if (seller.getStatus().getStatusid()!=1){
            map.put("code","2");
            map.put("msg","该账户"+seller.getStatus().getStatusname());
            return map;
        }

        request.getSession().setAttribute("sellerName",seller.getSellername());
        request.getSession().setAttribute("buyerName","");
        map.put("code","0");
        return map;
    }

    //注册
    @RequestMapping("registerSeller")
    @ResponseBody
    public Map<String,String> registerSeller(String checkCode,String userName, String userPhone, String userPwd,HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        //校验验证码是否正确
        if(!request.getSession().getAttribute("checkcode").equals(checkCode)){
            map.put("code","1");
            map.put("msg","验证码不正确，请重新输入");
            return map;
        }
        Seller seller = new Seller();
        seller.setSellername(userName);
        seller.setSellerphone(userPhone);
        seller.setSellerpwd(userPwd);
        sellerService.addSeller(seller);
        request.getSession().setAttribute("buyerName","");
        request.getSession().setAttribute("sellerName","");
        map.put("code","0");
        return map;
    }
}